package com.hieu.tripmanager;

import java.util.ArrayList;

import com.hieu.abstracts.TripActivity;
import com.hieu.adapter.DayAdapter;
import com.hieu.bean.Day;
import com.hieu.sql.DAL;
import com.hieu.util.Util;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class ViewTripActivity extends TripActivity {
	private ListView listDay;
	private DayAdapter adapter;
	private ArrayList<Day> mListDay;
	private DAL mDAL;
	private String idTrip = null;
	private String notes = null;
	private String start = null;
	private String end = null;
	private String type = null;
	private String name = null;
	private TextView txtNotes;
	private TextView txtStart;
	private TextView txtEnd;
	private TextView txtName;
	private TextView txtSum;
	private TextView txtEndDateTitle;
	private TextView txtTypeTrip;
	private boolean check;
	private Util util = new Util();
	private ImageView img1, img2, img3, img4, img5;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		initRootView();
		initData();
		initUIComponents();
		setListeners();
		loadData();
	}

	@Override
	protected void initData() {
		// Nhận data từ MyTripActivity gửi qua
		Intent intent = getIntent();
		if (intent != null) {
			idTrip = intent.getStringExtra("idTrip");
			notes = intent.getStringExtra("notes");
			start = intent.getStringExtra("start");
			end = intent.getStringExtra("end");
			type = intent.getStringExtra("type");
			name = intent.getStringExtra("name");
		}
		mDAL = new DAL(this);
		mListDay = new ArrayList<Day>();
		if (mDAL != null) {
			mListDay = mDAL.getDayList(idTrip);
			check = util.checkNotFinishTrip(idTrip, ViewTripActivity.this);
		}
	}

	@Override
	protected void initRootView() {
		setContentView(R.layout.view_trip_activity_layout);
	}

	@Override
	protected void initUIComponents() {
		listDay = (ListView) findViewById(R.id.listDay);
		View headerView = LayoutInflater.from(this).inflate(R.layout.layout_viewtrip_header, null);
		listDay.addHeaderView(headerView);
		txtNotes = (TextView) findViewById(R.id.txtNotes);
		txtStart = (TextView) findViewById(R.id.txtStartDate);
		txtEnd = (TextView) findViewById(R.id.txtEndDate);
		txtSum = (TextView) findViewById(R.id.txtSumMoneyTrip);
		txtName = (TextView) findViewById(R.id.txtNameTrip);
		txtTypeTrip = (TextView) findViewById(R.id.txtTypeTrip);
		txtEndDateTitle = (TextView) findViewById(R.id.endDate);

		img1 = (ImageView) findViewById(R.id.imageView1);
		img2 = (ImageView) findViewById(R.id.imageView2);
		img3 = (ImageView) findViewById(R.id.imageView3);
		img4 = (ImageView) findViewById(R.id.imageView4);
		img5 = (ImageView) findViewById(R.id.imageView5);

		txtNotes.setText("\"" + notes + "\"");
		txtStart.setText(start);
		txtEnd.setText(end);
		txtTypeTrip.setText(type);
		txtName.setText(name);
		setImage(idTrip);
		// Kiểm tra nếu chuyến đang thực hiện thì ẩn title của Ngày kết thúc
		if (check) {
			txtEndDateTitle.setVisibility(View.INVISIBLE);
		}
		
		txtSum.setText(util.convertMoney(mDAL.getSumMoneyTrip(idTrip)));

		

		adapter = new DayAdapter(this, R.layout.list_day_item, mListDay);
		listDay.setAdapter(adapter);

	}

	@Override
	protected void setListeners() {
		listDay.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent myIntent = new Intent(ViewTripActivity.this,
						ViewDayActivity.class);
				String day = mListDay.get(arg2 - 1).getDay();
				myIntent.putExtra("Day", day);
				startActivity(myIntent);

			}
		});

		listDay.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				mDAL.deleteDay(mListDay.get(position - 1).getDay());
				mListDay.remove(position - 1);
				adapter.notifyDataSetChanged();
				txtSum.setText(util.convertMoney(mDAL.getSumMoneyTrip(idTrip)));
				Toast.makeText(getBaseContext(), "Xóa ngày thành công",
						Toast.LENGTH_SHORT).show();
				return false;
			}
		});
	}

	@Override
	protected void loadData() {
		// TODO Auto-generated method stub

	}

	private void setImage(String idTrip) {
		ArrayList<Bitmap> listImg = new ArrayList<Bitmap>();
		if (mDAL != null) {
			listImg = mDAL.getImageTrip(idTrip);
		}
		if (listImg != null) {
			if (listImg.size() == 1) {
				img1.setImageBitmap(listImg.get(0));
				img2.setVisibility(View.INVISIBLE);
				img3.setVisibility(View.INVISIBLE);
				img4.setVisibility(View.INVISIBLE);
				img5.setVisibility(View.INVISIBLE);
			}
			if (listImg.size() == 2) {
				img1.setImageBitmap(listImg.get(0));
				img2.setImageBitmap(listImg.get(1));
				img3.setVisibility(View.INVISIBLE);
				img4.setVisibility(View.INVISIBLE);
				img5.setVisibility(View.INVISIBLE);
			}
			if (listImg.size() == 3) {
				img1.setImageBitmap(listImg.get(0));
				img2.setImageBitmap(listImg.get(1));
				img3.setImageBitmap(listImg.get(2));
				img4.setVisibility(View.INVISIBLE);
				img5.setVisibility(View.INVISIBLE);
			}
			if (listImg.size() == 4) {
				img1.setImageBitmap(listImg.get(0));
				img2.setImageBitmap(listImg.get(1));
				img3.setImageBitmap(listImg.get(2));
				img4.setImageBitmap(listImg.get(3));
				img5.setVisibility(View.INVISIBLE);
			}
			if (listImg.size() == 5) {
				img1.setImageBitmap(listImg.get(0));
				img2.setImageBitmap(listImg.get(1));
				img3.setImageBitmap(listImg.get(2));
				img4.setImageBitmap(listImg.get(3));
				img5.setImageBitmap(listImg.get(4));
			}

		}
	}

}
